package com.deng.etl.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.deng.etl.common.EtlAuto;
import com.deng.etl.common.IEtlInterface;
import com.deng.etl.common.EtlLog;

//删除   化疗药房
public class ImportPharmacy_count extends ImportBase implements IEtlInterface {

	
	public static String COLUMNS=" *  ";
	
	public static String SELECT="select "+COLUMNS+"  from JIXIAO_COMPOUNDINFO";
	
	public static String INSERT="insert into t_org_pharmacy_workload_month" +
			"(stat_time,create_time," +
			"org_operate_organization_id,org_operate_organization_name," +
			"count) " +
			"	values(?,?," +
			"'40P6','化疗药房'," +
			"?)";
	
	public static String DELETE="delete from t_org_pharmacy_workload_month where stat_time=? and org_operate_organization_id = '40P6' ";	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
	}

	
	public int getResultSet(ResultSet rs,String arr,int size){
		if(size<TOPN){
			return size;
		}
		int ret=0;
		String pri="";
		try {
			for(int i=size;i>0;i--){
				rs.absolute(i);
				if(!pri.equals(rs.getString(arr))&&i!=size){
					ret=i;
					break;
				}
				
				pri=rs.getString(arr);
			}
			rs.beforeFirst();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	
	public void insertData(Connection sourceConn,Connection targetConn,String name,String fromDate,String toDate) {
		System.out.println("从："+fromDate+"   到："+toDate);
		
		Date from=null;
		Date to=null;
		try {
			from = format_s.parse(fromDate);
			to=format_s.parse(toDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		String sql=SELECT+" where 配药月份= '"+
		(from.getYear()+1900)+"-"+((from.getMonth()+1)>=10?(from.getMonth()+1):("0"+(from.getMonth()+1)))+"'" +
				"	 ";
		System.out.println(sql);
		try {
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst=sourceConn.prepareStatement(sql);
			
			PreparedStatement pst_insert=targetConn.prepareStatement(INSERT);
			java.sql.Date date=new java.sql.Date(from.getYear(),from.getMonth(),1);
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
			
				pst_insert.setDate(1, date);
				Date datetmp=new Date();
				pst_insert.setTimestamp(2, getTimestamp(datetmp));
				pst_insert.setInt(3, rs.getInt(2));
					
				pst_insert.addBatch();
			}
			pst_insert.executeBatch();
			rs.close();
			System.out.println("s完成：  ");
			pst.close();
			pst_insert.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deleteData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		// TODO Auto-generated method stub
		System.out.println("从："+fromDate+"   到："+toDate);
		
		Date from=null;

		try {
			from = format_s.parse(fromDate);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst_delete=targetConn.prepareStatement(DELETE);
			java.sql.Date date=new java.sql.Date(from.getYear(),from.getMonth(),1);
			pst_delete.setDate(1,date);
			pst_delete.executeUpdate();
			pst_delete.close();
				
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}
		catch(SQLException e){
			
		}
		
	}

	public void updateData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			
			deleteData(sourceConn, targetConn,
					name, fromDate, toDate);
			insertData(sourceConn, targetConn,
					name, fromDate, toDate);
			
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}
}
